package com.tinshow.web.controller.test;

import com.tinshow.config.utils.GZIPUtil;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by randy on 16-12-15.
 */
@Controller
public class FanliBrandController {


    public String getRequestTmall(String value) throws IOException {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "http://super.fanli.com/"+value;
        HttpGet get = new HttpGet(url);
        get.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        get.setHeader("accept-encoding", "gzip, deflate, sdch");
        get.setHeader("Accept-Language","zh-CN,zh;q=0.8");
        get.setHeader("Connection","keep-alive");
        get.setHeader("Cache-Control","max-age=0");
        get.setHeader("Upgrade-Insecure-Requests","1");
        get.setHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
        get.setHeader("Host","uper.fanli.com");
        HttpResponse response = httpClient.execute(get);
        String result = "";
        if (response.getStatusLine().getStatusCode() == 200) {
//            result = EntityUtils.toString(response.getEntity(),"utf-8");
            Header h = response.getFirstHeader("Content-Encoding");
//            result = EntityUtils.toString(response.getEntity(), "gb2312");
            if (h != null) {
                String acceptEncodeing = h.getValue();
                if (StringUtils.isNotEmpty(acceptEncodeing) && acceptEncodeing.indexOf("gzip") != -1) {
                    InputStream is = response.getEntity().getContent();
                    byte[] bytes = IOUtils.toByteArray(is);
                    result = new String(GZIPUtil.unGZip(bytes), "utf-8");
                } else {
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                }

            } else {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
            System.out.print(result);
            parseHtml(result);
//            System.out.print(result);
            return result;
        }
        return "";
    }

    @RequestMapping(value = "/toFan")
    @ResponseBody
    public String getFan(@RequestParam(value = "urlName") String urlName) throws Exception{
        getRequestTmall(urlName);
        return "123123";
    }

    public String parseHtml(String result){
        Document doc = Jsoup.parse(result);
        Element J_logos_inner = doc.getElementById("J_logos_inner");
        Element div = J_logos_inner.getElementsByTag("div").get(0);
        Elements ul = div.getElementsByTag("ul");
        for(int i = 0;i < ul.size();i++){
            Elements li = ul.get(i).getElementsByTag("li");
            for (int j =0; j < li.size();j++){
                Element brand = li.get(j);
                String imageUrl = brand.getElementsByTag("a").get(0).getElementsByTag("img").get(0).getElementsByAttribute("src").text();
                System.out.print(imageUrl+"\n");
            }
        }
        return "";
    }

}
